'''
rectify the SfM model from SIMPLE_RADIAL to PINHOLE
'''
import os

command_1 = 'colmap image_undistorter --image_path={0} --input_path={1} --output_path={2}'
command_2 = 'colmap model_converter --input_path={0} --output_path={1} --output_type=TXT'
command_3 = 'mv {0} {1}'
command_4 = 'python sort_images_txt.py --reference={0} --unordered={1} --save_to={2}'

MegaDepth_v1_SfM = '/media/jiangwei/data_ssd/MegaDepth_v1_SfM/'
assert os.path.isdir(MegaDepth_v1_SfM), 'Change to your local path'
all_scenes = [
    '0000/sparse/manhattan/0',
    '0000/sparse/manhattan/1',
    '0001/sparse/manhattan/0',
    '0002/sparse/manhattan/0',
    '0003/sparse/manhattan/0',
    '0004/sparse/manhattan/0',
    '0004/sparse/manhattan/1',
    '0004/sparse/manhattan/2',
    '0005/sparse/manhattan/0',
    '0005/sparse/manhattan/1',
    '0007/sparse/manhattan/0',
    '0007/sparse/manhattan/1',
    '0008/sparse/manhattan/0',
    '0011/sparse/manhattan/0',
    '0012/sparse/manhattan/0',
    '0013/sparse/manhattan/0',
    '0015/sparse/manhattan/0',
    '0015/sparse/manhattan/1',
    '0016/sparse/manhattan/0',
    '0017/sparse/manhattan/0',
    '0019/sparse/manhattan/0',
    '0019/sparse/manhattan/1',
    '0020/sparse/manhattan/0',
    '0020/sparse/manhattan/1',
    '0021/sparse/manhattan/0',
    '0022/sparse/manhattan/0',
    '0023/sparse/manhattan/0',
    '0023/sparse/manhattan/1',
    '0024/sparse/manhattan/0',
    '0025/sparse/manhattan/0',
    '0025/sparse/manhattan/1',
    '0026/sparse/manhattan/0',
    '0027/sparse/manhattan/0',
    '0032/sparse/manhattan/0',
    '0032/sparse/manhattan/1',
    '0033/sparse/manhattan/0',
    '0034/sparse/manhattan/0',
    '0035/sparse/manhattan/0',
    '0036/sparse/manhattan/0',
    '0037/sparse/manhattan/0',
    '0039/sparse/manhattan/0',
    '0041/sparse/manhattan/0',
    '0041/sparse/manhattan/1',
    '0042/sparse/manhattan/0',
    '0043/sparse/manhattan/0',
    '0044/sparse/manhattan/0',
    '0046/sparse/manhattan/0',
    '0046/sparse/manhattan/1',
    '0046/sparse/manhattan/2',
    '0047/sparse/manhattan/0',
    '0048/sparse/manhattan/0',
    '0049/sparse/manhattan/0',
    '0050/sparse/manhattan/0',
    '0056/sparse/manhattan/0',
    '0057/sparse/manhattan/0',
    '0058/sparse/manhattan/0',
    '0058/sparse/manhattan/1',
    '0060/sparse/manhattan/0',
    '0061/sparse/manhattan/0',
    '0062/sparse/manhattan/0',
    '0062/sparse/manhattan/1',
    '0063/sparse/manhattan/0',
    '0063/sparse/manhattan/1',
    '0063/sparse/manhattan/2',
    '0063/sparse/manhattan/3',
    '0064/sparse/manhattan/0',
    '0065/sparse/manhattan/0',
    '0067/sparse/manhattan/0',
    '0070/sparse/manhattan/0',
    '0071/sparse/manhattan/0',
    '0071/sparse/manhattan/1',
    '0076/sparse/manhattan/0',
    '0078/sparse/manhattan/0',
    '0080/sparse/manhattan/0',
    '0083/sparse/manhattan/0',
    '0086/sparse/manhattan/0',
    '0087/sparse/manhattan/0',
    '0087/sparse/manhattan/1',
    '0090/sparse/manhattan/0',
    '0092/sparse/manhattan/0',
    '0092/sparse/manhattan/1',
    '0094/sparse/manhattan/0',
    '0095/sparse/manhattan/0',
    '0095/sparse/manhattan/1',
    '0095/sparse/manhattan/2',
    '0098/sparse/manhattan/0',
    '0099/sparse/manhattan/0',
    '0100/sparse/manhattan/0',
    '0101/sparse/manhattan/0',
    '0102/sparse/manhattan/0',
    '0103/sparse/manhattan/0',
    '0104/sparse/manhattan/0',
    '0104/sparse/manhattan/1',
    '0105/sparse/manhattan/0',
    '0107/sparse/manhattan/0',
    '0115/sparse/manhattan/0',
    '0117/sparse/manhattan/0',
    '0117/sparse/manhattan/1',
    '0117/sparse/manhattan/2',
    '0121/sparse/manhattan/0',
    '0121/sparse/manhattan/1',
    '0122/sparse/manhattan/0',
    '0129/sparse/manhattan/0',
    '0130/sparse/manhattan/0',
    '0130/sparse/manhattan/1',
    '0130/sparse/manhattan/2',
    '0133/sparse/manhattan/0',
    '0133/sparse/manhattan/1',
    '0137/sparse/manhattan/0',
    '0137/sparse/manhattan/1',
    '0137/sparse/manhattan/2',
    '0141/sparse/manhattan/0',
    '0143/sparse/manhattan/0',
    '0147/sparse/manhattan/0',
    '0147/sparse/manhattan/1',
    '0148/sparse/manhattan/0',
    '0148/sparse/manhattan/1',
    '0149/sparse/manhattan/0',
    '0150/sparse/manhattan/0',
    '0151/sparse/manhattan/0',
    '0156/sparse/manhattan/0',
    '0160/sparse/manhattan/0',
    '0160/sparse/manhattan/1',
    '0160/sparse/manhattan/2',
    '0162/sparse/manhattan/0',
    '0162/sparse/manhattan/1',
    '0168/sparse/manhattan/0',
    '0175/sparse/manhattan/0',
    '0176/sparse/manhattan/0',
    '0176/sparse/manhattan/1',
    '0176/sparse/manhattan/2',
    '0177/sparse/manhattan/0',
    '0178/sparse/manhattan/0',
    '0178/sparse/manhattan/1',
    '0181/sparse/manhattan/0',
    '0183/sparse/manhattan/0',
    '0185/sparse/manhattan/0',
    '0186/sparse/manhattan/0',
    '0189/sparse/manhattan/0',
    '0190/sparse/manhattan/0',
    '0197/sparse/manhattan/0',
    '0200/sparse/manhattan/0',
    '0200/sparse/manhattan/1',
    '0204/sparse/manhattan/0',
    '0204/sparse/manhattan/1',
    '0205/sparse/manhattan/0',
    '0205/sparse/manhattan/1',
    '0209/sparse/manhattan/1',
    '0212/sparse/manhattan/0',
    '0212/sparse/manhattan/1',
    '0214/sparse/manhattan/0',
    '0214/sparse/manhattan/1',
    '0217/sparse/manhattan/0',
    '0223/sparse/manhattan/0',
    '0223/sparse/manhattan/1',
    '0223/sparse/manhattan/2',
    '0224/sparse/manhattan/0',
    '0224/sparse/manhattan/1',
    '0229/sparse/manhattan/0',
    '0231/sparse/manhattan/0',
    '0235/sparse/manhattan/0',
    '0237/sparse/manhattan/0',
    '0238/sparse/manhattan/0',
    '0240/sparse/manhattan/0',
    '0243/sparse/manhattan/0',
    '0252/sparse/manhattan/0',
    '0257/sparse/manhattan/0',
    '0258/sparse/manhattan/0',
    '0265/sparse/manhattan/0',
    '0265/sparse/manhattan/1',
    '0269/sparse/manhattan/0',
    '0269/sparse/manhattan/1',
    '0269/sparse/manhattan/2',
    '0271/sparse/manhattan/0',
    '0275/sparse/manhattan/0',
    '0277/sparse/manhattan/0',
    '0277/sparse/manhattan/1',
    '0281/sparse/manhattan/0',
    '0285/sparse/manhattan/0',
    '0286/sparse/manhattan/0',
    '0286/sparse/manhattan/1',
    '0290/sparse/manhattan/0',
    '0290/sparse/manhattan/1',
    '0294/sparse/manhattan/0',
    '0299/sparse/manhattan/0',
    '0303/sparse/manhattan/0',
    '0306/sparse/manhattan/0',
    '0307/sparse/manhattan/0',
    '0312/sparse/manhattan/0',
    '0312/sparse/manhattan/1',
    '0323/sparse/manhattan/0',
    '0326/sparse/manhattan/0',
    '0327/sparse/manhattan/0',
    '0327/sparse/manhattan/1',
    '0327/sparse/manhattan/2',
    '0331/sparse/manhattan/0',
    '0335/sparse/manhattan/0',
    '0335/sparse/manhattan/1',
    '0341/sparse/manhattan/0',
    '0341/sparse/manhattan/1',
    '0348/sparse/manhattan/0',
    '0349/sparse/manhattan/0',
    '0349/sparse/manhattan/1',
    '0360/sparse/manhattan/0',
    '0360/sparse/manhattan/1',
    '0360/sparse/manhattan/2',
    '0366/sparse/manhattan/0',
    '0377/sparse/manhattan/0',
    '0380/sparse/manhattan/0',
    '0387/sparse/manhattan/0',
    '0389/sparse/manhattan/0',
    '0389/sparse/manhattan/1',
    '0394/sparse/manhattan/0',
    '0394/sparse/manhattan/1',
    '0402/sparse/manhattan/0',
    '0402/sparse/manhattan/1',
    '0406/sparse/manhattan/0',
    '0407/sparse/manhattan/0',
    '0411/sparse/manhattan/0',
    '0411/sparse/manhattan/1',
    '0412/sparse/manhattan/0',
    '0412/sparse/manhattan/1',
    '0412/sparse/manhattan/2',
    '0430/sparse/manhattan/0',
    '0430/sparse/manhattan/1',
    '0430/sparse/manhattan/2',
    '0443/sparse/manhattan/0',
    '0446/sparse/manhattan/0',
    '0455/sparse/manhattan/0',
    '0472/sparse/manhattan/0',
    '0472/sparse/manhattan/1',
    '0474/sparse/manhattan/0',
    '0474/sparse/manhattan/1',
    '0474/sparse/manhattan/2',
    '0476/sparse/manhattan/0',
    '0476/sparse/manhattan/1',
    '0476/sparse/manhattan/2',
    '0478/sparse/manhattan/0',
    '0478/sparse/manhattan/1',
    '0482/sparse/manhattan/0',
    '0493/sparse/manhattan/0',
    '0493/sparse/manhattan/1',
    '0494/sparse/manhattan/1',
    '0496/sparse/manhattan/0',
    '0505/sparse/manhattan/0',
    '0559/sparse/manhattan/0',
    '0733/sparse/manhattan/0',
    '0733/sparse/manhattan/1',
    '0768/sparse/manhattan/0',
    '0860/sparse/manhattan/0',
    '0860/sparse/manhattan/1',
    '1001/sparse/manhattan/0',
    '1017/sparse/manhattan/0',
    '1589/sparse/manhattan/0',
    '3346/sparse/manhattan/0',
    '4541/sparse/manhattan/0',
    '5000/sparse/manhattan/0',
    '5001/sparse/manhattan/0',
    '5002/sparse/manhattan/0',
    '5003/sparse/manhattan/0',
    '5004/sparse/manhattan/0',
    '5005/sparse/manhattan/0',
    '5006/sparse/manhattan/0',
    '5007/sparse/manhattan/0',
    '5008/sparse/manhattan/0',
    '5009/sparse/manhattan/0',
    '5010/sparse/manhattan/0',
    '5011/sparse/manhattan/0',
    '5012/sparse/manhattan/0',
    '5013/sparse/manhattan/0',
    '5014/sparse/manhattan/0',
    '5015/sparse/manhattan/0',
    '5016/sparse/manhattan/0',
    '5017/sparse/manhattan/0',
    '5018/sparse/manhattan/0',
]

with open('rectify.sh', "w") as fid:
    for s in all_scenes:
        s = os.path.join(MegaDepth_v1_SfM, s)
        new_dir = s + '_rectified'
        img_dir = s[:s.find('sparse')] + 'images'
        fid.write(command_1.format(img_dir, s, new_dir) + '\n')
        fid.write(command_2.format(new_dir + '/sparse', new_dir + '/sparse') + '\n')
        fid.write(command_3.format(new_dir + '/sparse/images.txt', new_dir + '/sparse/unorder_images.txt') + '\n')
        fid.write(command_4.format(s + '/images.txt', new_dir + '/sparse/unorder_images.txt', new_dir + '/sparse/images.txt') + '\n')
